using System;
using System.Collections;
using DaaseVarmer.helperClasses;
using DaaseVarmer.helperClasses.HardwareInterface;
using DaaseVarmer.helperClasses.networkComponents;
using DaaseVarmer.model;
using Microsoft.SPOT;

namespace DaaseVarmer.domain
{
    /*
     * The settings class holds all the variables there are used in the program
     */
    public class Settings 
    {
        private static Settings instance;

        private Settings()
        {
            InitializeSdCardSettings();
        }

        public static Settings Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Settings();
                }
                return instance;
            }
        }

        //List of outside temperatur readings
        public TemperatureReadings OutsiteTemp = new TemperatureReadings();

        //List of solarHeater temperatur readings
        public TemperatureReadings SolarHeaterTemp = new TemperatureReadings();

        //sets if manuel control of the solarheater is activated
        public bool ManuelOverride { get; set; }

        //sets the manuel overside motor speed 1-10
        //TODO implement manuel set of motor speed
        public int MotorSpeed { get; set; }

        //pachube feed 1 - OutsiteTemp
        public string PachubeFeedStream_OutsiteTemp = "OutsiteTemp";

        //pachube feed 2 - SolarHeaterTemp
        public string PachubeFeedStream_SolarHeaterTemp = "SolarHeaterTemp";

        //pachube key loaded from the sd card
        public string PachubeApiKey { get; private set; }

        //pachube feedID loaded from the sd card
        public string PachubeFeedID { get; private set; }

        // The minimum temperature where the motor should start
        private int _minTemperatureMotorStart;
        public int MinTemperatureMotorStart
        {
            get { return _minTemperatureMotorStart; }
            set
            {
                _minTemperatureMotorStart = value;
                //SaveSetingsToSd();
            }
        }

        //The time between temperautur samples and ventilator changes
        private int _sampleTime;
        public int SampleTime
        { 
            get { return _sampleTime; }  
            set 
            { 
                _sampleTime = value;
                //SaveSetingsToSd();
            } 
        }

        //The time now gotten from internet or rtc device
        public DateTime TimeNow { get; private set; }

        // this is used to see if the time is the real world time 
        public Boolean isRealTime { get; private set; }

        //gets all the data from the settings in the sd card and the time
        public bool GetNetworkRealTime()
        {
            TimeHelper time = new TimeHelper();
            try
            {
                TimeNow = time.GetNetworkTime();
                return isRealTime = true;
            }
            catch (Exception)
            {
                return isRealTime = false;
            }

        }

        //Loads the settings into the variables
        private void InitializeSdCardSettings()
        {
            //SdAccess sdAccess = new SdAccess();
            /*Hashtable settings = sdAccess.getSettingsFromSdCArd();

            if (sdAccess.SettingsFileExist() && false)
            {
                //get the data
                PachubeApiKey = (string)settings["PachubeApiKey"];
                PachubeFeedID = (string)settings["PachubeFeedID"];
                MinTemperatureMotorStart = (int)settings["MinTemperatureMotorStart"];
                SampleTime = (int)settings["SampleTime"];
            }*/
            if (true)
            {
                //default values if no settings is imported
                PachubeApiKey = "11111111111111111111111111111111";
                PachubeFeedID = "11111";
                MinTemperatureMotorStart = 25;  // degrees celcius
                SampleTime = 10000;             //5 sec
                ManuelOverride = false;
                MotorSpeed = 0;
            }
        }

        private void SaveSetingsToSd()
        {
            //SdAccess sdAccess = new SdAccess();
            //sdAccess.saveSetingsToSd();
        }

    }
}
